package com.diandi.signature;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * Created by mxp on 2017/11/27.
 */
@PropertySource("classpath:signatureJdbc.properties")
@Configuration
public class DruidMybatisConfiguration implements EnvironmentAware {

    private Environment env;

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }

    /**
     * 创建数据源
     * @return
     * @throws Exception
     */
    @Bean(name = "signature-dataSource")
    public DataSource getMasterDataSource() throws Exception {
        Properties props = new Properties();
        props.put("driverClassName", env.getProperty("db.driverClassName"));
        props.put("url", env.getProperty("db.signature.url"));
        props.put("username", env.getProperty("db.signature.username"));
        props.put("password", env.getProperty("db.signature.password"));
        props.put("initialSize", env.getProperty("db.signature.initialSize"));
        props.put("maxActive", env.getProperty("db.signature.maxActive"));
        props.put("minIdle", env.getProperty("db.signature.minIdle"));
        props.put("maxWait", env.getProperty("db.signature.maxWait"));
        props.put("validationQuery", env.getProperty("db.signature.validationQuery"));
        props.put("testOnBorrow", env.getProperty("db.signature.testOnBorrow"));
        props.put("testOnReturn", env.getProperty("db.signature.testOnReturn"));
        props.put("testWhileIdle", env.getProperty("db.signature.testWhileIdle"));
        props.put("timeBetweenEvictionRunsMillis", env.getProperty("db.signature.timeBetweenEvictionRunsMillis"));
        props.put("minEvictableIdleTimeMillis", env.getProperty("db.signature.minEvictableIdleTimeMillis"));
        props.put("removeAbandoned", env.getProperty("db.signature.removeAbandoned"));
        props.put("removeAbandonedTimeout", env.getProperty("db.signature.removeAbandonedTimeout"));
        props.put("logAbandoned", env.getProperty("db.signature.logAbandoned"));
        props.put("filters", env.getProperty("db.signature.filters"));
        return DruidDataSourceFactory.createDataSource(props);
    }

    /**
     * 创建SqlSessionFactory
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "signature-sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("signature-dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sfb = new SqlSessionFactoryBean();
        sfb.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        sfb.setMapperLocations(resourceResolver.getResources("classpath*:/xmlMapper/*.xml"));
        return sfb.getObject();
    }

    @Bean(name = "signature-sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("signature-sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name = "signature-mapperScan")
    public MapperScannerConfigurer mapperScannerConfig() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionTemplateBeanName("signature-sqlSessionTemplate");
        mapperScannerConfigurer.setBasePackage("com.diandi.signature.mapper");
        mapperScannerConfigurer.setAnnotationClass(Repository.class);
        return mapperScannerConfigurer;
    }

    /**
     * 事务
     */
    @Bean(name = "signature-transaction")
    public DataSourceTransactionManager transactionManager(@Qualifier("signature-dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}